<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>引出生命周期</title>
    <style>
        .title{color:red}
    </style>
</head>
<body>
    <!-- 准备好一个容器 -->
    <div id="test"></div>


    <!-- 引入react核心库 -->
    <script src="../js/16/react.development.js" ></script>
    <!-- 引入react-dom，用于支持react操作DOM -->
    <script src="../js/16/react-dom.development.js"></script>
    <!-- 引入babel，用于将jsx转为js -->
    <script src="../js/babel.js" ></script>
    <script type="text/babel"> /*此处一定要写babel，否则无法识别jsx语法*/
        // 创建组件
        class Life extends React.Component {
            state = { opacity: 1 }
            death = () => {
                // 卸载组件
                ReactDOM.unmountComponentAtNode(document.getElementById('test'))
            }
            // 组件挂在完毕
            componentDidMount () {
                this.timer = setInterval(() => {
                    let opacity = this.state.opacity
                    opacity -= 0.1
                    if (opacity <= 0) {
                        opacity = 1
                    }
                    this.setState({ opacity })
                }, 200)
            }
            // 组件将要卸载
            componentWillUnmount () {
                // 清除定时器
                clearInterval(this.timer)
            }
            // 初始化渲染、状态更新之后、属性更新后
            render () {
                return (
                    <div>
                        <h2 style={{opacity:this.state.opacity}}>文字渐渐变淡</h2>
                        <button onClick={this.death}>unmountComponent</button>
                    </div>
                )
            }
        }
        ReactDOM.render(<Life />, document.getElementById('test'))
    </script>
</body>
</html>